package gravitationally_adjustable_physical_ball;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class DataShower extends JPanel implements ChangeListener {
    private JPanel area_g=new JPanel();
    private JPanel area_diameter=new JPanel();
    private JSlider gSetter;
    private JSlider diameterSetter;
    private JTextField gShower=new JTextField();
    private JTextField diameterShower=new JTextField();
    private AppPanel panel;
    public DataShower(AppPanel panel){
        this.panel=panel;
        gShower.setPreferredSize(new Dimension(200,30));
        diameterShower.setPreferredSize(new Dimension(200,30));
        gShower.setEditable(false);
        diameterShower.setEditable(false);
        gSetter=new JSlider(0,100,Ball.getG());
        diameterSetter=new JSlider(20,100,Ball.getDiameter());
        gSetter.addChangeListener(this);
        diameterSetter.addChangeListener(this);
        gShower.setText("重力加速度:"+gSetter.getValue());
        diameterShower.setText("小球直径:"+diameterSetter.getValue());
        this.setLayout(new BorderLayout(2,2));
        area_diameter.setLayout(new BorderLayout(2,2));
        area_g.setLayout(new BorderLayout(2,2));
        area_diameter.add(diameterShower,BorderLayout.WEST);
        area_diameter.add(diameterSetter,BorderLayout.EAST);
        area_g.add(gShower,BorderLayout.WEST);
        area_g.add(gSetter,BorderLayout.EAST);
        this.add(area_g,BorderLayout.NORTH);
        this.add(area_diameter,BorderLayout.SOUTH);
        gShower.setBackground(Color.GRAY);
        diameterShower.setBackground(Color.GRAY);
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        if(e.getSource()==gSetter){
            Ball.setG(gSetter.getValue());
            gShower.setText("重力加速度:"+ gSetter.getValue());
        }
        else if (e.getSource()==diameterSetter){
            panel.getBall().setX(panel.getBall().getX()- (double) (diameterSetter.getValue()- Ball.getDiameter())/2);
            panel.getBall().setY(panel.getBall().getY()- (double) (diameterSetter.getValue()- Ball.getDiameter())/2);
            Ball.setDiameter(diameterSetter.getValue());
            panel.repaint();
            diameterShower.setText("小球直径:"+ diameterSetter.getValue());
        }
        else {
            System.out.println("Abnormal operation detected.");
        }
    }
}
//初计划是把相关参数显示与调节放到这里